package cn.fuxue.hrms.common.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author fuxueHu
 * @description: md5加密
 * @date 2023/12/9 19:51
 */
public class Md5 {
    private static final String [] digital={
      "0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"
    };
    public static String md5(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        String encry=null;
        MessageDigest md5=MessageDigest.getInstance("MD5");
        if(password!=null){
            encry="";
            byte []bytes=md5.digest(password.getBytes("utf-8"));
            for(byte b:bytes){
                int num=b;
                if(num<0){
                    num+=256;
                }
                int index1=num/16;
                int index2=num%16;
                encry+=digital[index1]+digital[index2];
            }
        }
        return encry;
    }
    public static String encryByMD5(String txt) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        return md5(md5(txt)+"hrms");
    }

    public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        String pwd="007";
        System.out.println(encryByMD5(pwd));
    }
}
